// 导入创建仓库模块
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
// 导入推断仓库类型 1.导入ts
import type { RootState } from "./index";

// 解密
// import crypt from "@/utils/crypto";
import Taro from "@tarojs/taro";
// 这里是改模块的state的类型 1.接口
interface User {
  user: UserObject;
  user_token: AccessToken;
  // isLogin: boolean;
  wxInfo: wxInfo;
  openid: string;
  carList: Datum[];
  member_id: string;
  showKeyboard: boolean;
  bgMount: boolean;
}


// 获取浏览器存储
// 默认值
// 微信用户accessToken
const getUser = {
  access_token: "",
  expires_in: 2,
  refresh_token: "",
  openid: "",
  scope: "",
};
// 微信用户信息
const wxInfo = {
  openid: "",
  nickName: "",
  sex: -1,
  avatarUrl: Taro.getStorageSync("img") || "",
};
// 用户信息
const user = {
  id: -1,
  status: -1,
  openid: "",
  mobile: "",
  code: "",
  name: "",
  sex: 0,
  birthday: "1970-01-01",
  memo: null,
  free_parking_expire_at: "",
  created_at: "",
  updated_at: "",
  member_car_id: -1,
  member_car_plate_number: "",
};


const checkOpenId = (name?: string) => {
  const encryptedUserToken = Taro.getStorageSync("openid");

  if (encryptedUserToken) {
    // const decryptedUserToken: { openid?: string } =
    //   crypt.decrypt(encryptedUserToken);

    if (encryptedUserToken) {
      // 在解密成功后，可以直接访问解密后的属性
      // 例如，如果 decryptedUserToken 是 { openid: 'someValue' }，
      // 则 decryptedUserToken.openid 将包含 'someValue'
      // console.log("解密的" + name, decryptedUserToken);
      return encryptedUserToken;
    } else {
      console.error("Decryption failed or result is empty.");
    }
  }

  // 如果没有加密的用户令牌或解密失败，返回一个空对象
  return "" ;
};

// 这里是改模块的state的类型 2.实例
const user_token: User = {
  // user_token: checkUserToken("encryptedUserToken"),
  user_token: getUser,
  // 用户填写
  user: user,
  // 微信用户信息
  wxInfo: wxInfo,
  // isLogin: false,
  openid: checkOpenId() || "",
  carList: [],
  member_id: "",
  showKeyboard: false,
  bgMount: true,
};

// 实例模块
const systemSlice = createSlice({
  //模块命名
  name: "systemSlice",
  //初始值 // 3.使用
  initialState: user_token,
  //方法
  reducers: {
    // 使用PayloadAction类型来接收整个对象
    changeUserToken(state, action: PayloadAction<AccessToken>) {
      // 直接将整个对象赋值给state
      state.user_token = action.payload;
    },

    changeWxInfo(state, action: PayloadAction<wxInfo>) {
      // 直接将整个对象赋值给state
      // console.log(action.payload, "action");
      state.wxInfo = action.payload;
    },
    // changeUser(state, params) {
    //   state.user = params;
    // },
    changeUser(state, action: PayloadAction<UserObject>) {
      // 直接将整个对象赋值给state
      console.log(action.payload, "changeUser");
      state.user = action.payload;
    },
    changeCarList(state, action: PayloadAction<Datum[]>) {
      // 直接将整个对象赋值给state
      state.carList = action.payload;
      console.log("更新", action.payload);
      
    },
    // changeIsLogin(state) {
    //   state.isLogin = !state.isLogin;
    // },
    // member_id
    changeMember_id(state, params) {
      state.member_id = params.payload;
    },
    changeOpenid(state, params) {
      console.log(params.payload, "params.changeOpenid");
      state.openid = params.payload;
    },
    changeBgMount(state, params) {
      state.bgMount = params.payload;
      console.log(params.payload);
    },
    //
    changeShowKeyboard(state, params) {
      state.showKeyboard = params.payload;
      console.log(params.payload);
    },
  },
});


// 导出方法
export const {
  changeUserToken,
  // changeIsLogin,
  changeUser,
  changeCarList,
  changeWxInfo,
  changeOpenid,
  changeMember_id,
  changeShowKeyboard,
  changeBgMount
} = systemSlice.actions;

// 导入推断仓库类型 2.使用ts
// 方便使用者去使用state
// 导出属性
export const myUserToken = (state: RootState) => state.user.user_token;
// export const storeIsLogin = (state: RootState) => state.user.isLogin;
export const storeUser = (state: RootState) => state.user.user;//
export const storeMember_id = (state: RootState) => state.user.member_id;//
export const showKeyboard = (state: RootState) => state.user.showKeyboard;//
export const bgMount = (state: RootState) => state.user.bgMount;//
export const carList = (state: RootState) => state.user.carList;
export const myWxInfo = (state: RootState) => state.user.wxInfo;
export const phoneOpenid = (state: RootState) => state.user.openid;
export default systemSlice.reducer;

// 获取用户信息
// const checkWxInfo = (name: string) => {
//   const encryptedUserToken = localStorage.getItem(name);

//   if (encryptedUserToken) {
//     const decryptedUserToken: AccessToken = JSON.parse(
//       JSON.stringify(crypt.decrypt(encryptedUserToken))
//     );
//     console.log("解密的" + name, decryptedUserToken);
//     // console.log("解密的 user_token.openid:", decryptedUserToken.openid);
//     return decryptedUserToken;
//   }
//   return wxInfo;
// };